package gruppe7.servlets;

import gruppe7.data.UserController;
import gruppe7.tlrs.FantomCalculator;
import gruppe7.tlrs.TLRSControl;
import gruppe7.util.Html;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TicketPrice extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private Html htmlInclude = new Html();

    public TicketPrice() {
        super();

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        RequestDispatcher rd = request.getRequestDispatcher("main_links.jsp");
        UserController uc = UserController.getInstance();

        out.println(htmlInclude.setHeader("Lookup Ticket Price"));
        rd.include(request, response);

        out.println("<div id ='content'>");
        out.println("<div id='content-left'>");
        out.println("<h3><img src='images/tickets.png' /> Ticket Lookups</h3><br/>");
        String departure = "";
        String destination = "";
        if (uc.isValidSession(request)) {
            departure = (String) request.getParameter("departure");
            destination = (String) request.getParameter("destination");
            if (departure.equals(destination)) {
                RequestDispatcher forwardDisp = getServletContext().getRequestDispatcher("/LookUpPrice");
                request.setAttribute("same", true);
                forwardDisp.forward(request, response);
            }
            TLRSControl tickC = TLRSControl.getInstance();
            int departureZone = tickC.getZone(departure);
            int destinationZone = tickC.getZone(destination);
            int totalZones = 0;

            if (departureZone > destinationZone) {
                totalZones = departureZone - destinationZone;
            } else {
                totalZones = destinationZone - departureZone;
            }

            int totalFantoms = FantomCalculator.getFantoms(totalZones);
            out.println("<p> The price to travel from " + departure + " to " + destination + ", will cost you " + totalFantoms + " Fantoms. </br>"
                    + "Have a safe trip!</p>");
            out.println("<form action='PurchaseTicket' method='post'>"
                    + "<input type='hidden' id='departure' name='departure' value='" + departure + " '/>"
                    + "<input type='hidden' id='destination' name='destination' value='" + destination + " '/>"
                    + "<input type='hidden' id='departure' name='price' value='" + totalFantoms + " '/>"
                    + "<input class='submit' type='submit'value='buy ticket'> "
                    + "</form>");
            out.println("<br /><p><a href='LookUpPrice'>Look up another price - click here</a></p>");

        }

        out.println("</div></div>");
        out.println(htmlInclude.setFooter());


    }
}
